import { _decorator, Component, Node, input, Input, Sprite, SpriteFrame, resources, EventTouch } from 'cc';
import { AudioMgr } from './AudioMgr';
const { ccclass, property } = _decorator;

@ccclass('volumeControl')
export class volumeControl extends Component {


    muteImg: string = 'images/volum_mute'
    unmuteImg: string = 'images/volum_unmute'


    @property(SpriteFrame)
    muteSprite: SpriteFrame;
    @property(SpriteFrame)
    unmuteSprite: SpriteFrame;


    // 声音状态
    volumeState: number = 1

    start() {
        this.node.on(Input.EventType.TOUCH_END, this.volume, this)
    }


    update(deltaTime: number) {

    }

    volume(event: EventTouch) {

        console.log(event.touch);
        
        if (this.volumeState) {
            this.volumeState = 0
            this.mute()
        } else {
            this.volumeState = 1
            this.unmute()
        }
    }

    /**
     * 静音
     */
    mute() {
        AudioMgr.inst.mute()
        this.getComponent(Sprite).spriteFrame = this.unmuteSprite;

        /* resources.load(this.unmuteImg, SpriteFrame, (err: any, spriteFrame) => {
            const sprite = this.node.getComponent(Sprite);
            sprite.spriteFrame = spriteFrame;
        }); */
    }

    /**
     * 取消静音
     */
    unmute() {
        AudioMgr.inst.unmute(1)
        this.getComponent(Sprite).spriteFrame = this.muteSprite;

        /* resources.load(this.unmuteImg, SpriteFrame, (err: any, spriteFrame) => {
            const sprite = this.node.getComponent(Sprite);
            sprite.spriteFrame = spriteFrame;
        }); */
    }

}


